package javabase.collection.list.listsort;

import java.text.CollationKey;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

class Postgraduate implements Comparable<Postgraduate>
{
	private String name;
	private int score;
	private int age;
	
	public Postgraduate() {
		
	}
	
	public Postgraduate(String name, int score, int age) {
		this.setName(name);
		this.setScore(score);
		this.setAge(age);
	}

	public int compareTo(Postgraduate stu) {
		
		Collator zhCollator = Collator.getInstance(Locale.CHINA);
		CollationKey[] keys = new CollationKey[2];
		keys[0] = zhCollator.getCollationKey(this.name);
		keys[1] = zhCollator.getCollationKey(stu.name);
		int tmp = 0;
		if ((tmp = keys[0].compareTo(keys[1])) != 0) {
			return tmp;
		} else {
			if(this.score > stu.score) {
				return -1;
			} else if(this.score < stu.score) {
				return 1;
			} else {
				if(this.age > stu.age) {
					return 1;
				} else if(this.age == stu.age) {
					return 0;
				} else {
					return -1;
				}
			}
		}
		
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getScore() {
		return score;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getAge() {
		return age;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", score=" + score + ", age=" + age + "]";
	}
	
}

public class ComparableDemo02 {
	
	public static void main(String[] args) {
		
		List<Postgraduate> listStu = new ArrayList<Postgraduate>();
		listStu.add(new Postgraduate("路人甲",96,10));
		listStu.add(new Postgraduate("路人乙",99,11));
		listStu.add(new Postgraduate("路人丙",55,9));
		listStu.add(new Postgraduate("路人丁",55,11));
		listStu.add(new Postgraduate("路人戊",99,19));
		Collections.sort(listStu);
		
		System.out.println("排序之后：");
		for(int i=0; i<listStu.size(); i++) {
			System.out.println(listStu.get(i));
		}
		
		System.out.println("-------------------华丽的分割线------------------");
		Postgraduate [] stu = new Postgraduate[5];
		stu[0] = new Postgraduate("小明",96,10);
		stu[1] = new Postgraduate("小王",99,11);
		stu[2] = new Postgraduate("小孙",55,9);
		stu[3] = new Postgraduate("小李",55,11);
		stu[4] = new Postgraduate("小猛",99,19);
		System.out.println("排序之前：");
		for(int i=0;i<stu.length;i++) {
			System.out.println(stu[i]);
		}
		
//		Arrays.sort(stu, 0, 1); //对前2个排序
		Arrays.sort(stu);
		System.out.println("排序之后：");
		for(int i=0;i<stu.length;i++) {
			System.out.println(stu[i]);
		}
		
//		List<Student> list = Arrays.asList(stu); //list.toArray();
//		for (int i = 0, sum = list.size(); i < sum; i++) {
//			System.out.println(stu[i]);
//		}
		
	}

}
